﻿using System.Security.Claims;
using Core.Models.LoginUser;

namespace Core.Framework.CommonControllers;

public static class ClaimsPrincipalExtentions
{
    public static UserModel GetCurrentUser(this ClaimsPrincipal user)
    {
        var userModel = new UserModel();
        userModel.UserId = Convert.ToInt64(user.Claims.FirstOrDefault(p => p.Type.Equals("UserId", StringComparison.OrdinalIgnoreCase)).Value);
        userModel.UserName = user.Claims.FirstOrDefault(p => p.Type.Equals("UserName", StringComparison.OrdinalIgnoreCase)).Value;
        userModel.DepartmentId = Convert.ToInt64(user.Claims.FirstOrDefault(p => p.Type.Equals("Department", StringComparison.OrdinalIgnoreCase)).Value);
        userModel.JobNumber = user.Claims.FirstOrDefault(p => p.Type.Equals("JobNumber", StringComparison.OrdinalIgnoreCase)).Value;
        userModel.RoleIds = Newtonsoft.Json.JsonConvert.DeserializeObject<List<long>>(user.Claims.FirstOrDefault(p => p.Type.Equals("Roles", StringComparison.OrdinalIgnoreCase)).Value);
        return userModel;
    }
}
